{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f1c559f8-4bc3-4655-a4c0-59661290c259",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T03:10:41.801905Z",
     "iopub.status.busy": "2022-05-20T03:10:41.801594Z",
     "iopub.status.idle": "2022-05-20T03:10:42.058310Z",
     "shell.execute_reply": "2022-05-20T03:10:42.057263Z",
     "shell.execute_reply.started": "2022-05-20T03:10:41.801836Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50bef551-990e-4b97-9a58-c78bdfc39db7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T03:10:44.049374Z",
     "iopub.status.busy": "2022-05-20T03:10:44.049046Z",
     "iopub.status.idle": "2022-05-20T03:10:44.064594Z",
     "shell.execute_reply": "2022-05-20T03:10:44.063956Z",
     "shell.execute_reply.started": "2022-05-20T03:10:44.049348Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key1</th>\n",
       "      <th>Key2</th>\n",
       "      <th>Key3</th>\n",
       "      <th>Key4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Beijing</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guangzhou</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Key1  Key2  Key3  Key4\n",
       "Beijing       0     1     2     3\n",
       "Shanghai      4     5     6     7\n",
       "Guangzhou     8     9    10    11"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验对象\n",
    "data = pd.DataFrame(\n",
    "    np.arange(12).reshape(3, 4),\n",
    "    index=[\"Beijing\", \"Shanghai\", \"Guangzhou\"],\n",
    "    columns=[\"Key1\", \"Key2\", \"Key3\", \"Key4\"],\n",
    ")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2afe0245-6de3-46fb-9c3a-47e3871dd1cf",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 一、采用数据转换修改索引标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8b466e56-e1ca-4d22-97f9-cffcd33cac28",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T03:11:45.970697Z",
     "iopub.status.busy": "2022-05-20T03:11:45.970338Z",
     "iopub.status.idle": "2022-05-20T03:11:45.978405Z",
     "shell.execute_reply": "2022-05-20T03:11:45.977145Z",
     "shell.execute_reply.started": "2022-05-20T03:11:45.970647Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['BEIJING', 'SHANGHAI', 'GUANGZHOU'], dtype='object')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重命名data的行标签，全部字母大写\n",
    "data.index.map(lambda x: x.upper())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "af0675f6-903d-4cc0-82c8-06a09d71bfd9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T03:11:50.071686Z",
     "iopub.status.busy": "2022-05-20T03:11:50.071347Z",
     "iopub.status.idle": "2022-05-20T03:11:50.083877Z",
     "shell.execute_reply": "2022-05-20T03:11:50.082756Z",
     "shell.execute_reply.started": "2022-05-20T03:11:50.071661Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key1</th>\n",
       "      <th>Key2</th>\n",
       "      <th>Key3</th>\n",
       "      <th>Key4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BEIJING</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SHANGHAI</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GUANGZHOU</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Key1  Key2  Key3  Key4\n",
       "BEIJING       0     1     2     3\n",
       "SHANGHAI      4     5     6     7\n",
       "GUANGZHOU     8     9    10    11"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重命名data的行标签，全部字母大写，赋值到原对象中\n",
    "data.index = data.index.map(lambda x: x.upper())\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c68f09d2-a38e-4990-8c89-d33f4df3238f",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 二、采用rename修改索引标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd8e6345-b0d5-475a-b7f8-1c020d67e7f7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T03:13:04.970680Z",
     "iopub.status.busy": "2022-05-20T03:13:04.970102Z",
     "iopub.status.idle": "2022-05-20T03:13:04.982047Z",
     "shell.execute_reply": "2022-05-20T03:13:04.981367Z",
     "shell.execute_reply.started": "2022-05-20T03:13:04.970653Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>KEY1</th>\n",
       "      <th>KEY2</th>\n",
       "      <th>KEY3</th>\n",
       "      <th>KEY4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Beijing</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guangzhou</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           KEY1  KEY2  KEY3  KEY4\n",
       "Beijing       0     1     2     3\n",
       "Shanghai      4     5     6     7\n",
       "Guangzhou     8     9    10    11"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# rename新建对象后修改标签名\n",
    "data.rename(index=str.title, columns=str.upper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5b10b0eb-8319-4d43-8ef6-5552154e0095",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T07:31:02.184105Z",
     "iopub.status.busy": "2022-05-20T07:31:02.183676Z",
     "iopub.status.idle": "2022-05-20T07:31:02.195421Z",
     "shell.execute_reply": "2022-05-20T07:31:02.194653Z",
     "shell.execute_reply.started": "2022-05-20T07:31:02.184079Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key1</th>\n",
       "      <th>Key2</th>\n",
       "      <th>Key3</th>\n",
       "      <th>Key4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>北京</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SHANGHAI</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GUANGZHOU</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Key1  Key2  Key3  Key4\n",
       "北京            0     1     2     3\n",
       "SHANGHAI      4     5     6     7\n",
       "GUANGZHOU     8     9    10    11"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# rename新建对象后修改标签名，结合字典对象\n",
    "data.rename(index={\"BEIJING\": \"北京\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "164be54f-dc70-4dae-aff8-26cfe15bb206",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T07:39:51.888092Z",
     "iopub.status.busy": "2022-05-20T07:39:51.887577Z",
     "iopub.status.idle": "2022-05-20T07:39:51.899183Z",
     "shell.execute_reply": "2022-05-20T07:39:51.898072Z",
     "shell.execute_reply.started": "2022-05-20T07:39:51.888051Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key1</th>\n",
       "      <th>Key2</th>\n",
       "      <th>Key3</th>\n",
       "      <th>Key4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>北京</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SHANGHAI</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GUANGZHOU</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Key1  Key2  Key3  Key4\n",
       "北京            0     1     2     3\n",
       "SHANGHAI      4     5     6     7\n",
       "GUANGZHOU     8     9    10    11"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# rename新建对象后修改标签名，结合字典对象，直接修改原对象\n",
    "data.rename(index={\"BEIJING\": \"北京\"}, inplace=True)\n",
    "data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
